<?php

namespace App\Admin\Controllers;

use App\Models\Supplier;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;

class SupplierController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '供应商';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Supplier());

        $grid->number('序号');
        $grid->rows(function ($row, $number) {
            $row->column('number', $number+1);
        });
        $grid->column('company', __('供应商名称'));
        $grid->column('number', __('供应商编号'));
        $grid->column('status')->switch([
            'on' =>  ['value' => 0, 'text' => '启用', 'color' => 'primary'],
            'off' => ['value' => 1, 'text' => '未启用', 'color' => 'default']
        ]);
        $grid->column('mobilephone', __('手机号码'));
        $grid->column('phone', __('电话号码'));
        $grid->column('contact', __('联系人'));
        $grid->column('created_at', __('创建时间'));
        $grid->column('updated_at', __('更新时间'));

        $grid->filter(function($filter){
            // 去掉默认的id过滤器
            $filter->disableIdFilter();
            // 在这里添加字段过滤器
            $filter->like('supplier_name', '供应商名称');
            $filter->like('contact', '联系人');
            $filter->equal('supplier_no', '供应商编号');
            $filter->equal('state','状态')->radio([
                ''   => 'All',
                0    => '启用',
                1    => '未启用',
            ]);
            $filter->equal('mobilephone', '手机号码');
            $filter->equal('phone', '电话号码');
        });

        return $grid;
    }

    /**
     * Edit interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
//    public function edit($id, Content $content)
//    {
//        return $content
//            ->header('供应商')
//            ->description('编辑')
//            ->body($this->form()->edit($id));
//    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(Supplier::findOrFail($id));

        $show->field('id', __('Id'));
        $show->field('supplier_name', __('供应商名称'));
        $show->field('supplier_no', __('供应商编号'));
//        $show->field('state', __('状态'))->states([
//            'on' =>   ['value' => 0, 'text' => '启用', 'color' => 'primary'],
//            'off' =>  ['value' => 1, 'text' => '未启用', 'color' => 'default']
//        ]);
        $show->state('状态')->using([
            '1' => '未启用',
            '0' => '启用'
        ]);
        $show->field('mobilephone', __('手机号码'));
        $show->field('phone', __('电话号码'));
        $show->field('contact', __('联系人'));
        $show->field('province', __('省'));
        $show->field('city', __('市'));
        $show->field('distrcit', __('区'));
        $show->field('detail_address', __('详细地址'));
        $show->field('remark', __('备注'));
        $show->field('created_at', __('创建时间'));
        $show->field('updated_at', __('更新时间'));

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Supplier());

        $form->hidden('id', 'ID');
        $form->text('company', __('公司名称'));

        $form->text('number', __('供应商编号'))
            ->append('<a type="button" id="get_number">生成编码</a>')
            ->setScript("
 $('#get_number').on('click', function() {
    $.ajax({
        type:'post',
        url: 'get_supplierno',
        data:{
            _token:LA.token
        },
        success: function (res) {
            $('#number').val(res);
            toastr.success('生成成功');
        }
    });
});")->readonly();
        $form->switch('status', __('状态'))
            ->states([
            'on' =>   ['value' => 0, 'text' => '启用', 'color' => 'primary'],
            'off' =>  ['value' => 1, 'text' => '未启用', 'color' => 'default']
        ]);

        $form->text('mobile', __('手机号码'))
            ->rules('required|regex:/^1[34578]{1}\d{9}$/',['regex'=>'请输入正确的手机号吗']);
        $form->mobile('phone', __('电话号码'));
//            ->rules('required|regex:/^([0-9]{3,4}-)?[0-9]{7,8}$/',['regex'=>'请输入正确的电话']);

        $form->text('name', __('联系人'));

        $form->distpicker(['province', 'city', 'county'], '地址选择');

        $form->text('address', __('详细地址'));
        $form->textarea('description', __('备注'));

        return $form;
    }

    /**
     * 获取随机16位供应商编号
     */
    public function get_supplierno()
    {
        $a = range(0,9);
        for($i=0;$i<16;$i++){
            $b[] = array_rand($a);
        }
        return join("",$b);
    }
}
